package com.zuul.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author yujiang
 * @description 网关过滤器
 * @date 2019/7/25 16:24
 */
@Component
public class JYZuulFilter extends ZuulFilter {


    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        RequestContext rc = RequestContext.getCurrentContext();
        if (Objects.isNull(rc.getRequest().getParameter("name"))) {
            rc.setSendZuulResponse(false);
            try {
                rc.getResponse().getWriter().write(" name cannot be null ...");
            } catch (Exception e) {
            }
            return null;
        }
        return null;
    }

}
